var api = require('../config/api.js');
var app = getApp();
var openId = wx.getStorageSync('openId');

function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
 * 封装微信的的request
 */
function request(url, data = {}, method = "GET") {
  return new Promise(function(resolve, reject) {
    wx.request({
      url: url,
      data: data,
      method: method,
      header: {
        'Content-Type': 'application/json',
        'X-Litemall-Token': wx.getStorageSync('token')
      },
      success: function(res) {

        if (res.statusCode == 200) {

          // if (res.data.errno == 501) {
          //   // 清除登录相关内容
          //   console.log('清除相关内容')
          //   try {
          //     wx.removeStorageSync('userInfo');
          //     wx.removeStorageSync('token');
          //     wx.removeStorageSync('openId');
          //   } catch (e) {
          //     // Do something when catch error
          //   }
          //   // 切换到登录页面
          //   wx.navigateTo({
          //     url: '/pages/auth/login/login'
          //   });
          // } else {
          //   resolve(res.data);
          // }
          resolve(res.data);
        } else {
          reject(res.errMsg);
        }

      },
      fail: function(err) {
        reject(err)
      }
    })
  });
}


//get请求
function networkget(url, params, app) {
  return new Promise(function (resolve, reject){
    wx.request({
      url: url,
      header: {
        'Content-Type': 'application/json',
      },
      data: params,
      method: 'GET',
      success: function (res) {

        if (res.statusCode == 200) {
          resolve(res.data);
        } else {
          reject(res.errMsg);
        }

      },
      fail: function (err) {
        reject(err)
      }
    })
  });

} 

function redirect(url) {

  //判断页面是否需要登录
  if (false) {
    wx.redirectTo({
      url: '/pages/auth/login/login'
    });
    return false;
  } else {
    wx.redirectTo({
      url: url
    });
  }
}

function showErrorToast(msg) {
  wx.showToast({
    title: msg,
    image: '/static/images/icon_error.png'
  })
}

module.exports = {
  formatTime,
  request,
  redirect,
  showErrorToast,
  networkget,
  openId
}